package com.example.user_service.user_contorller;


import com.example.user_service.otherservice.ActivityService;
import com.example.user_service.otherservice.GoodsServiceOpenFeign;
import com.example.user_service.otherservice.OrderService;
import com.example.user_service.otherservice.Otherservice;
import com.example.user_service.return_data.Mall_Data;
import com.example.user_service.service.Lservice;
import com.example.user_service.vo.*;
import com.netflix.hystrix.contrib.javanica.annotation.DefaultProperties;
import com.netflix.hystrix.contrib.javanica.annotation.HystrixCommand;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiModelProperty;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.openfeign.FeignClient;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.client.RestTemplate;
import springfox.documentation.spring.web.json.Json;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;

/**
 * @author XiaoLiu
 * @date 2021-12-01 16:26
 **/
@RestController
@Api
@DefaultProperties(defaultFallback = "Demotion")
public class LController {
    @Autowired
    Otherservice otherservice;//远程调用菜单服务

    @Autowired
    Lservice lservice;

    @Autowired
    GoodsServiceOpenFeign goodsopenfeign;//远程调用商品服务

    @Autowired
    OrderService orderService;//远程调用订单服务

    @Autowired
    ActivityService activityService;//远程调用活动服务

    /* 利用openFeign远程调用菜单服务查询用户个人中心菜单
     * @author Liu
     * @date 2021/12/13 8:34
     * @param null
     * @return
     * @throws
     * @since
     */
    @RequestMapping("/MenuSelect")
    @ApiOperation("查询个人中心菜单")
    public Mall_Data MenuSelect()
    {
         return otherservice.get(20);
    }

    /* 用于后台管理员添加修改时选择的角色类型而用
     * @author Liu
     * @date 2021/12/14 10:26
     * @param null
     * @return
     * @throws
     * @since
     */
    @RequestMapping("/SelectRole")
    @ApiOperation("查询后台管理员")
    public Mall_Data RoleSelect()
    {
        return otherservice.SelectRole("管理员");
    }

    /* 查询管理员表
     * @author Liu
     * @date 2021/12/14 14:52
     * @param null
     * @return
     * @throws
     * @since
     */
    @RequestMapping("/AdministratorSelect")
    @ApiOperation("查询管理员表")
    public Mall_Data AdministratorSelect(@ApiParam("开始日")@RequestParam(value = "start",required = false) String createdatestr
    ,                                    @ApiParam("截止日")@RequestParam(value = "end",required = false) String enddatestr
    ,                                    @ApiParam("管理员账号")@RequestParam(value = "account",required = false) String account)
    {
        Mall_Data mall_data = new Mall_Data();
        mall_data.setcode(200);
        mall_data.setdata(lservice.AdministratorSelect(createdatestr,enddatestr,account));
        return mall_data;
    }

    /* 校验管理员账号是否存在
     * @author Liu
     * @date 2021/12/14 14:52
     * @param null
     * @return
     * @throws
     * @since
     */
    @RequestMapping("/check_Administrator")
    @ApiOperation("校验管理员账号是否存在")
    public Mall_Data check_Administrator(@ApiParam("管理员对象")@RequestBody Administrator administrator)
    {
        Mall_Data mall_data = new Mall_Data();
        if (lservice.check_Administrator(administrator)){
            //设置状态码
            mall_data.setcode(200);
            //设置error信息
            mall_data.seterror("not error");
            //设置数据信息
            mall_data.setdata("账号没重复");
        }else {
            //设置状态码
            mall_data.setcode(520);
            //设置error信息
            mall_data.seterror("账号重复");
        }
        return mall_data;
    }


    /* 创建管理人员
     * @author Liu
     * @date 2021/12/14 14:52
     * @param null
     * @return
     * @throws
     * @since
     */
    @RequestMapping("/create_Administrator")
    @ApiOperation("创建管理人员")
    public Mall_Data create_Administrator(@ApiParam("管理员对象")@RequestBody Administrator administrator)
    {
        Mall_Data mall_data = new Mall_Data();
        if (lservice.create_Administrator(administrator)==2){
            //设置状态码
            mall_data.setcode(200);
            //设置error信息
            mall_data.seterror("not error");
            //设置数据信息
            mall_data.setdata("创建成功");
        }else {
            //设置状态码
            mall_data.setcode(520);
            //设置error信息
            mall_data.seterror("创建失败");
        }
        return mall_data;
    }

    /* 删除管理人员
     * @author Liu
     * @date 2021/12/14 14:52
     * @param null
     * @return
     * @throws
     * @since
     */
    @RequestMapping("/delete_Administrator")
    @ApiOperation("删除管理人员")
    public Mall_Data delete_Administrator(@ApiParam("管理员对象")@RequestBody Administrator administrator)
    {
        Mall_Data mall_data = new Mall_Data();
        mall_data.setdata( lservice.delete_Administrator(administrator));
        return mall_data;
    }

    /* 修改管理人员
     * @author Liu
     * @date 2021/12/14 14:52
     * @param null
     * @return
     * @throws
     * @since
     */
    @RequestMapping("/update_Administrator")
    @ApiOperation("修改管理人员")
    public Mall_Data update_Administrator(@ApiParam("管理员对象")@RequestBody Administrator administrator)
    {
        Mall_Data mall_data = new Mall_Data();
        mall_data.setdata( lservice.update_Administrator(administrator));
        return mall_data;
    }

    /* 修改管理人员状态
     * @author Liu
     * @date 2021/12/14 14:52
     * @param null
     * @return
     * @throws
     * @since
     */
    @RequestMapping("/update_Administrator_state")
    @ApiOperation("修改管理人员状态")
    public Mall_Data update_Administrator_state(@ApiParam("管理员对象")@RequestBody Administrator administrator)
    {
        Mall_Data mall_data = new Mall_Data();
        mall_data.setdata( lservice.update_Administrator_state(administrator));
        return mall_data;
    }

    /* 查询用户积分
     * @author Liu
     * @date 2021/12/14 14:52
     * @param null
     * @return
     * @throws
     * @since
     */
    @RequestMapping("/select_member_existing_integral")
    @ApiOperation("查询用户积分")
    public Mall_Data select_member_existing_integral(@ApiParam("用户id")@RequestParam("userid") int userid)
    {

        return new Mall_Data().setdata(lservice.select_member_existing_integral(userid).getMember_existing_integral());
    }

    /* 查询会员折扣
     * @author Liu
     * @date 2021/12/21 20:00
     * @param null
     * @return
     * @throws
     * @since
     */
    @RequestMapping("/select_user_member_zeko")
    @ApiOperation("查询会员折扣")
    public Mall_Data select_user_member_zeko(@ApiParam("用户id")@RequestParam("userid") int userid)
    {

        return new Mall_Data().setdata(lservice.select_user_member_zeko(userid).getMember_welfare_discount());
    }


    /* 修改用户积分
     * @author Liu
     * @date 2021/12/14 14:52
     * @param null
     * @return
     * @throws
     * @since
     */
    @RequestMapping("/update_member_existing_integral")
    @ApiOperation("修改用户积分")

    public Mall_Data update_member_existing_integral(@ApiParam("用户id")@RequestParam("userid") int userid,
                                                     @ApiParam("积分格式例如-500或+500")@RequestParam(value = "jifen",required = false)String str,@RequestBody Mingxi mingxi)
    {
        System.out.println(mingxi);
        return new Mall_Data().setdata(lservice.update_member_existing_integral(userid,str,mingxi));
    }




    /* 查询我的足迹
     * @author Liu
     * @date 2021/12/14 14:52
     * @param null
     * @return
     * @throws
     * @since
     */
    @RequestMapping("/select_my_tracks")
    @ApiOperation("查询我的足迹")
    public Mall_Data select_my_tracks(@ApiParam("用户id")@RequestParam("userid") int userid)
    {
        //查询足迹
        List<MyTracks>list = lservice.select_my_tracks(userid);
        //为空返回
        if (list.isEmpty())
        return new Mall_Data().setcode(200).seterror("select == null");

        //将商品对象添加到足迹对象中
        list.forEach((val)->
        {
            List<LinkedHashMap>list1 = (List<LinkedHashMap>) goodsopenfeign.selectgoods((int) val.getFootprint_id()).get("data");
            val.setT_commodity(list1.get(0));
        });
        //将足迹按照日期分类
        List<MyTracks>list2 = new ArrayList<>();
        list.forEach((v)->
        {
          if (list2.isEmpty())
          {
             list2.add(v);
          }else
          {
              boolean a = false;
              for (MyTracks v1 : list2)
              {
                      if (v1.getTracks_create().equals(v.getTracks_create()))
                      {
                          v1.add(v);
                          break;
                      }else
                  {

                      for (MyTracks v2 : list2) {
                          if (v2.getTracks_create().equals(v.getTracks_create())) {
                              a = true;
                              v2.add(v);
                              break;
                          }
                      }
                      if (!a)
                      list2.add(v);
                      break;
                  }
              }
          }
        });

        return new Mall_Data().setcode(200).setdata(list2);
    }

    /* 删除我的足迹
     * @author Liu
     * @date 2021/12/17 14:39
     * @param null
     * @return
     * @throws
     * @since
     */
    @RequestMapping("/delete_my_tracks")
    @ApiOperation("根据id删除我的足迹")
    public Mall_Data delete_my_tracks(@ApiParam("足迹id")@RequestParam("tracks_id") int tracks_id)
    {
        return new Mall_Data().setdata(lservice.delete_my_tracks(tracks_id));
    }

    /* 根据时间来删除
     * @author Liu
     * @date 2021/12/19 17:03
     * @param null
     * @return
     * @throws
     * @since
     */
    @RequestMapping("/delete_my_tracks_date")
    @ApiOperation("根据时间删除我的足迹")
    public Mall_Data delete_my_tracks_date(@ApiParam("用户id")@RequestParam("userid") int userid,
                                           @ApiParam("时间")@RequestParam("date1")String date)
    {

        return new Mall_Data().setdata(lservice.delete_my_tracks_date(userid,date.replaceFirst("删除当天","")));
    }



    /* 新增我的足迹
     * @author Liu
     * @date 2021/12/17 14:39
     * @param null
     * @return
     * @throws
     * @since
     */
    @RequestMapping("/insert_my_tracks")
    @ApiOperation("新增我的足迹")
    public Mall_Data insert_my_tracks(@ApiParam("用户id")@RequestParam(value = "user_id",required = false) int user_id,
                                      @ApiParam("商品id")@RequestParam(value = "footprint_id",required = false) int footprint_id,
                                      @ApiParam("商品分类id")@RequestParam(value = "goods_type",required = false) int goods_type
                                      )
    {

        return new Mall_Data().setdata(lservice.insert_my_tracks(user_id,footprint_id,goods_type));
    }


    /* 查看用户收藏
     * @author Liu
     * @date 2021/12/19 22:37
     * @param null
     * @return
     * @throws
     * @since
     */
    @RequestMapping("/select_collect_goods")
    @ApiOperation("查看用户收藏")
    public Mall_Data select_collect_goods(@ApiParam("用户id")@RequestParam("userid") int userid)
    {

        //查询收藏
        List<CollectGoods>list = lservice.select_collect_goods(userid);
        //为空返回
        if (list.isEmpty())
            return new Mall_Data().setcode(200).seterror("select == null");

        //将商品对象添加到足迹对象中
        list.forEach((val)->
        {
            List<LinkedHashMap>list1 = (List<LinkedHashMap>) goodsopenfeign.selectgoods((int) val.getCommodity_id()).get("data");
            val.setT_commodity(list1.get(0));
        });


        return new Mall_Data().setcode(200).setdata(list);
    }

    /* 删除用户收藏
     * @author Liu
     * @date 2021/12/19 22:37
     * @param null
     * @return
     * @throws
     * @since
     */
    @RequestMapping("/delete_collect_goods")
    @ApiOperation("删除用户收藏")
    public Mall_Data delete_collect_goods(@ApiParam("收藏id")@RequestParam("goods_id") int goods_id)
    {

        return new Mall_Data().setdata(lservice.delete_collect_goods(goods_id));

    }

    /* 新增用户收藏
     * @author Liu
     * @date 2021/12/20 8:10
     * @param null
     * @return
     * @throws
     * @since
     */
    @RequestMapping("/insert_collect_goods")
    @ApiOperation("新增用户收藏")
    public Mall_Data insert_collect_goods(@ApiParam("用户id")@RequestParam("user_id") int user_id,
                                      @ApiParam("商品id")@RequestParam("footprint_id") int footprint_id)
    {
        if (lservice.select_collect_goods1(user_id,footprint_id).isEmpty())
        return new Mall_Data().setdata(lservice.insert_collect_goods(user_id,footprint_id));
        return new Mall_Data().setcode(500).seterror("商品已添加");
    }


    /* 查询用户个人主页详情信息
     * @author Liu
     * @date 2021/12/20 10:56
     * @param null
     * @return
     * @throws
     * @since
     */
    @RequestMapping("/select_user_welcome")
    @ApiOperation("查询用户个人主页详情信息")
    public Mall_Data select_user_welcome(@ApiParam("用户id")@RequestParam("user_id")int user_id)
    {

        List<User_welcome>list = lservice.select_user_welcome(user_id);
        list.get(0).setOrderstatus_pl_size(orderService.taiochass(user_id));
        list.get(0).setOrderstatus_zhifu_size(orderService.taiocha(user_id));
        list.get(0).setNumber_of_coupons(activityService.searchhb(user_id).size());
        list.get(0).setShoc(lservice.select_collect_goods(user_id).size());
        return new Mall_Data().setcode(200).setdata(list.get(0));
    }



    /* 熔断处理降级
     * @author Liu
     * @date 2021/12/9 14:20
     * @param null
     * @return
     * @throws
     * @since
     */
    public Mall_Data Demotion() {
        System.out.println("你报错了.....看看隔壁服务挂了没.......没有就是你的方法有问题........");
        return null;
    }


}
